//
//  ZYCategoryViewController.m
//  Adorable_Things
//
//  Created by huzhiyan on 16/1/11.
//  Copyright © 2016年 Banana Inc. All rights reserved.
//

#import "ZYCategoryViewController.h"
#import "ZYCategoryMainCollectionViewCell.h"
#import "ZYCategoryMainModel.h"
#import "ZYCategorySecondPageController.h"
#import "ZYCategorySearchViewController.h"
#import "ZYCategoryHeaderView.h"

@interface ZYCategoryViewController () <UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) ZYCategoryHeaderView *headerView;


@end

@implementation ZYCategoryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationController.navigationBar.hidden = YES;
    [self addSubCollectionViewItemSize];
    [self addSubHeaderView];
    [self setUpData];
}

#pragma mark ---  添加导航自定义视图
// 添加导航自定义视图
- (void)addSubHeaderView
{
    _headerView = [[ZYCategoryHeaderView alloc] initWithFrame:CGRectMake(0, 20, kWidth, 45)];
    _headerView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_headerView];
    [_headerView.backItemButton removeFromSuperview];
    
    UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
    button.frame = CGRectMake(20, 2, kWidth - 40, 38);
    [button addTarget:self action:@selector(headerViewButtonClick:) forControlEvents:(UIControlEventTouchUpInside)];
    button.layer.cornerRadius = 20;
    button.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"LaunchImage-568h@2x"]];
    [button setImage:[UIImage imageNamed:@"iconfont-sousuo"] forState:(UIControlStateNormal)];
    button.adjustsImageWhenHighlighted = NO;
    [button setImageEdgeInsets:UIEdgeInsetsMake(5, (kWidth - 40) / 2 + 14, 5,  (kWidth - 40) / 2 + 14)];
    [_headerView addSubview:button];
}

- (void)headerViewButtonClick:(UIButton *)button
{
    ZYCategorySearchViewController *searchVC = [[ZYCategorySearchViewController alloc] init];
    searchVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:searchVC animated:YES];
}

#pragma mark ---  请求数据
// 请求数据
- (void)setUpData
{
    NSMutableDictionary *parentDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"", @"parent_id", nil];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager POST:@"http://api.menghuoapp.com/v1/category/list" parameters:parentDic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic  = (NSDictionary *)responseObject;
        NSArray *dataArr = dic[@"data"];
        _dataArray = [NSMutableArray array];
        for (NSDictionary *dataDic in dataArr) {
            ZYCategoryMainModel *model = [[ZYCategoryMainModel alloc] init];
            [model setValuesForKeysWithDictionary:dataDic];
            self.ID = model.category_id;
            [_dataArray addObject:model];
        }
        [_collectionView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}

#pragma mark ---  添加collectionView
- (void)addSubCollectionViewItemSize
{
    self.collectionView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"LaunchImage-568h@2x"]];
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    self.collectionView.collectionViewLayout = flowLayout;
    flowLayout.itemSize = CGSizeMake((kWidth - 80) / 3, (kWidth - 80) / 3);
    flowLayout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);
    [self.collectionView registerNib:[UINib nibWithNibName:@"ZYCategoryMainCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"ZYMainCollectionViewCell"];

}

#pragma mark ---  UICollectionViewDelegate,UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZYCategoryMainCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ZYMainCollectionViewCell" forIndexPath:indexPath];
    ZYCategoryMainModel *model = _dataArray[indexPath.row];
    cell.model = model;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZYCategoryMainModel *model = _dataArray[indexPath.row];
    ZYCategorySecondPageController *secondVC = [[ZYCategorySecondPageController alloc] init];
    secondVC.category_id = model.category_id;
    secondVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:secondVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
